﻿//********************************************************************************//
//创建日期：<创建日期，2016-4-13>
//页面创建：<孙雪婷，sunxueting@shanghai3h.com>
//后台逻辑：<孙雪婷，sunxueting@shanghai3h.com>2016-06-13
//功能说明：银行帐处理
//********************************************************************************//
using BLL;
using BLL.Access;
using Entity;
using System;
using System.Data;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.YongShui.ZhangWuGL
{
    public partial class YinHangZCL : BasePage
    {

        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int YinHangZ_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["_YinHangZ_ID"]);
            }
            set
            {
                ViewState["_YinHangZ_ID"] = value;
            }
        }
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtYinHangZ
        {
            get
            {
                return ViewState["dtYinHangZ"] as DataTable;
            }
            set
            {
                ViewState["dtYinHangZ"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 银行帐处理 预收记录BLL
        /// </summary>
        YS_SF_YUSHOUJLBLL _YS_SF_YUSHOUJLBLL = new YS_SF_YUSHOUJLBLL();

        /// <summary>
        /// 银行帐处理 预收记录
        /// </summary>
        YS_SF_YUSHOUJL _YS_SF_YUSHOUJL = new YS_SF_YUSHOUJL();

        /// <summary>
        /// 银行账处理 科目
        /// </summary>
        YS_ZW_KEMUBLL _YS_ZW_KEMUBLL = new YS_ZW_KEMUBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                //Display();
            }
            gvYinHangZCL.DataSource = this.dtYinHangZ;
            gvYinHangZCL.DataBind();
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            if (txtZhangWuNY.Text.Trim()!="") 
            {
                LoadData();
            }
           
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnTianJia_Click(object sender, EventArgs e)
        {
            //初始化数据
            InitData();
            this.Status = GridViewStatus.Add;
            Display();
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDaYin_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBaoCun_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                SaveData();
            }
        }

        /// <summary>
        /// grid放弃
        /// </summary>
        protected void btnFangQi_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
        }

        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.YinHangZ_ID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
           // Display();
            SetData();
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            this.YinHangZ_ID = Convert.ToInt32(e.CommandArgument);
            _YS_SF_YUSHOUJL = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJL(this.YinHangZ_ID, null);           
            //执行逻辑删除            
            //_YS_JX_XINGZHENGQ.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            //_YS_JX_XINGZHENGQ.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _YS_SF_YUSHOUJL.I_JLZT = -1;
            _SYS_LOGBLL.Log<YS_SF_YUSHOUJL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "银行帐处理", OperateState.Delete, _YS_SF_YUSHOUJL, null);
            if (_YS_SF_YUSHOUJLBLL.InsertOrUpdate(_YS_SF_YUSHOUJL))
            {
                
                LoadData();
                PageTools.AlertMsg(this, "删除成功！");
            }
        }

        /// <summary>
        /// 保存验证
        /// </summary>
        /// <returns></returns>
        private bool ValidateData()
        {
            return true;
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            int i_zwny = BusinessTool.GetZWYM();
            txtZhangWuNY.Text = i_zwny.ToString();
            DataTable dt_kemu = _YS_ZW_KEMUBLL.SelectYS_ZW_KEMUsDynamic("I_TYPE IN (2,3)", null);
            for (int i = 0; i < dt_kemu.Rows.Count;i++ ) 
            {
                string type=dt_kemu.Rows[i]["I_BIANHAO"].ToString();
                if (type == "420" || type == "426" || type == "405" || type == "406" || type == "424" || type == "425")
                {
                    cmbKeMu.Items.Add(dt_kemu.Rows[i]["I_BIANHAO"] + "-" + dt_kemu.Rows[i]["S_MINGCHENG"], dt_kemu.Rows[i]["I_BIANHAO"]);
                    cbmKeMu2.Items.Add(dt_kemu.Rows[i]["I_BIANHAO"] + "-" + dt_kemu.Rows[i]["S_MINGCHENG"], dt_kemu.Rows[i]["I_BIANHAO"]);
                }
            }
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            int i_zwny = Convert.ToInt32(txtZhangWuNY.Text);
            int i_kemu = Convert.ToInt32(cmbKeMu.Value);
            int check = chbShouGongCLZM.Checked == true ? 1 : 0;
            //string con = "i_jlzt=0 and I_ZWYM={0} and I_MANA={1}";
            //if (i_kemu != 0)
            //{
            //    con += " AND I_TYPE={2} ";
            //}
            //this.dtYinHangZ = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJLsDynamic(string.Format(con, i_zwny, check, i_kemu), "D_SHURURQ desc");
            this.dtYinHangZ = _SpHelper.P_YSZW_YINHANGZCL(i_zwny, i_kemu, check).Tables[0];
            gvYinHangZCL.DataSource = this.dtYinHangZ;
            gvYinHangZCL.DataBind();
            txtZongJinE.Text = "";
            decimal zje = 0;
            for(int i=0 ;i<this.dtYinHangZ.Rows.Count;i++)
            {
                zje += Convert.ToDecimal(this.dtYinHangZ.Rows[i]["N_JINE"]);
            }
            txtZongJinE.Text=zje.ToString();
            lbZongYeStab1.Text = "共" + this.dtYinHangZ.Rows.Count.ToString() + "条";
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    //btnTianJia.Visible = true;
                    //btnDaYin.Visible = true;
                    btnChaXun.Enabled = true;
                    GridViewContentTable.Visible = false;
                    this.gvYinHangZCL.Enabled = true;
                    break;
                case GridViewStatus.Search:
                    //btnTianJia.Visible = true;
                    //btnDaYin.Visible = true;
                    btnChaXun.Enabled = true;
                    GridViewContentTable.Visible = true;
                    break;
                case GridViewStatus.Update:
                    //btnTianJia.Visible = false;
                    //btnDaYin.Visible = false;
                    btnChaXun.Enabled = false;
                    GridViewContentTable.Visible = true;
                    this.gvYinHangZCL.Enabled = false;
                   // this.lblOperateTip.Text = "修改";
                    //this.txtBianHao.Enabled = false;
                    //this.txtMingCheng.Focus();
                    break;
                case GridViewStatus.Add:
                    //btnTianJia.Visible = false;
                    //btnDaYin.Visible = false;
                    btnChaXun.Enabled = false;
                    GridViewContentTable.Visible = true;
                    this.gvYinHangZCL.Enabled = false;
                    //this.lblOperateTip.Text = "添加";
                    //this.txtBianHao.Enabled = true;
                   // this.txtBianHao.Focus();
                    break;
            }
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //添加
            if (this.Status == GridViewStatus.Add)
            {
                _YS_SF_YUSHOUJL.ID = 0;
                _YS_SF_YUSHOUJL.I_JLZT = 0;
                operateState = OperateState.Add;
            }
            //修改
            else
            {
                //修改操作
                _YS_SF_YUSHOUJL = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJL(this.YinHangZ_ID, null);
                operateState = OperateState.Modify;
            }

            _YS_SF_YUSHOUJL.I_TYPE = Convert.ToInt32(cbmKeMu2.Value);
            _YS_SF_YUSHOUJL.N_JINE = Convert.ToDouble( txtJinE.Text);
            _YS_SF_YUSHOUJL.S_BEIZHU =txtBeiZhu.Text;
            _YS_SF_YUSHOUJL.I_MANA = chkShouGongCLZM2.Checked == true ? 1 : 0;
            _YS_SF_YUSHOUJL.I_JLZT = 0;
            _YS_SF_YUSHOUJL.I_ZWYM = Convert.ToDecimal(txtZhangWuNY.Text);
            _YS_SF_YUSHOUJL.D_SHURURQ = BusinessTool.GetDataBaseDateTime();
            //this._YS_JX_XINGZHENGQ.S_CAOZUOR = this.UserID.ToString();
            //this._YS_JX_XINGZHENGQ.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _SYS_LOGBLL.Log<YS_SF_YUSHOUJL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "银行帐处理", operateState, _YS_SF_YUSHOUJL, null);
            if (_YS_SF_YUSHOUJLBLL.InsertOrUpdate(_YS_SF_YUSHOUJL, null))
            {
                
                PageTools.AlertMsg(this, "保存成功！");
                this.Status = GridViewStatus.Init;
                //Display();
                LoadData();
                //定位记录
                this.YinHangZ_ID = Web.Class.PageTools.FocusOpreateRecord(this.dtYinHangZ, this.gvYinHangZCL, "ID", _YS_SF_YUSHOUJL.ID.ToString(), true);
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
        }

        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            _YS_SF_YUSHOUJL = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJL(this.YinHangZ_ID, null);
            cbmKeMu2.Text = _YS_SF_YUSHOUJL.I_TYPE.ToString();
            txtJinE.Text = _YS_SF_YUSHOUJL.N_JINE.ToString();
            txtBeiZhu.Text = _YS_SF_YUSHOUJL.S_BEIZHU;
            if (_YS_SF_YUSHOUJL.I_MANA == 1)
            {
                chkShouGongCLZM2.Checked = true;
            }
            else 
            {
                chkShouGongCLZM2.Checked = false;
            }
            
        }

        #endregion        

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {

            if (e.Parameter == "edit")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                int i_zwny = Convert.ToInt32(txtZhangWuNY.Text);
                int i_kemu = Convert.ToInt32(cmbKeMu.Value);
                int check = chbShouGongCLZM.Checked == true ? 1 : 0;
                this.dtYinHangZ = _SpHelper.P_YSZW_YINHANGZCL(i_zwny, i_kemu, check).Tables[0];
                decimal zje = 0;
                for (int i = 0; i < this.dtYinHangZ.Rows.Count; i++)
                {
                    zje += Convert.ToDecimal(this.dtYinHangZ.Rows[i]["N_JINE"]);
                }

                _YS_SF_YUSHOUJL = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJL(id, null);
                e.Result = string.Join("|", e.Parameter, "0", "success", _YS_SF_YUSHOUJL.I_TYPE.ToString(), _YS_SF_YUSHOUJL.N_JINE.ToString(),
                    _YS_SF_YUSHOUJL.S_BEIZHU, _YS_SF_YUSHOUJL.I_MANA.ToString(), zje.ToString(), this.dtYinHangZ.Rows.Count.ToString());
            }
            else if (e.Parameter == "del")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                _YS_SF_YUSHOUJL = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJL(id, null);
                //执行逻辑删除            
                //_YS_JX_XINGZHENGQ.S_CAOZUOR = LoginInfo.CurrentUser.Account;
                //_YS_JX_XINGZHENGQ.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                _YS_SF_YUSHOUJL.I_JLZT = -1;
                _SYS_LOGBLL.Log<YS_SF_YUSHOUJL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "银行帐处理", OperateState.Delete, _YS_SF_YUSHOUJL, null);
                if (_YS_SF_YUSHOUJLBLL.InsertOrUpdate(_YS_SF_YUSHOUJL))
                {
                    
                    
                }
                int i_zwny = Convert.ToInt32(txtZhangWuNY.Text);
                int i_kemu = Convert.ToInt32(cmbKeMu.Value);
                int check = chbShouGongCLZM.Checked == true ? 1 : 0;
                this.dtYinHangZ = _SpHelper.P_YSZW_YINHANGZCL(i_zwny, i_kemu, check).Tables[0];
                decimal zje = 0;
                for (int i = 0; i < this.dtYinHangZ.Rows.Count; i++)
                {
                    zje += Convert.ToDecimal(this.dtYinHangZ.Rows[i]["N_JINE"]);
                }
                e.Result = e.Parameter + "|0|操作成功！|" + zje + "|" + this.dtYinHangZ.Rows.Count; ;
            }
            else if (e.Parameter == "save")
            {
                OperateState operateState;
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                //添加
                if (id <= 0)
                {
                    _YS_SF_YUSHOUJL.ID = 0;
                    _YS_SF_YUSHOUJL.I_JLZT = 0;
                    operateState = OperateState.Add;
                }
                //修改
                else
                {
                    //修改操作
                    _YS_SF_YUSHOUJL = _YS_SF_YUSHOUJLBLL.SelectYS_SF_YUSHOUJL(id, null);
                    operateState = OperateState.Modify;
                }
                _YS_SF_YUSHOUJL.I_TYPE = Convert.ToInt32(cbmKeMu2.Value == null || cbmKeMu2.Value == "" ? cbmKeMu2.Text : cbmKeMu2.Value);
                _YS_SF_YUSHOUJL.N_JINE = Convert.ToDouble(txtJinE.Text);
                _YS_SF_YUSHOUJL.S_BEIZHU = txtBeiZhu.Text;
                _YS_SF_YUSHOUJL.I_MANA = chkShouGongCLZM2.Checked == true ? 1 : 0;
                _YS_SF_YUSHOUJL.I_JLZT = 0;
                _YS_SF_YUSHOUJL.I_ZWYM = Convert.ToDecimal(txtZhangWuNY.Text);
                _YS_SF_YUSHOUJL.D_SHURURQ = BusinessTool.GetDataBaseDateTime();
                //this._YS_JX_XINGZHENGQ.S_CAOZUOR = this.UserID.ToString();
                //this._YS_JX_XINGZHENGQ.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                _SYS_LOGBLL.Log<YS_SF_YUSHOUJL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "银行帐处理", operateState, _YS_SF_YUSHOUJL, null);
                if (_YS_SF_YUSHOUJLBLL.InsertOrUpdate(_YS_SF_YUSHOUJL, null))
                {
                    int i_zwny = Convert.ToInt32(txtZhangWuNY.Text);
                    int i_kemu = Convert.ToInt32(cmbKeMu.Value);
                    int check = chbShouGongCLZM.Checked == true ? 1 : 0;
                    this.dtYinHangZ = _SpHelper.P_YSZW_YINHANGZCL(i_zwny, i_kemu, check).Tables[0];
                    decimal zje = 0;
                    for (int i = 0; i < this.dtYinHangZ.Rows.Count; i++)
                    {
                        zje += Convert.ToDecimal(this.dtYinHangZ.Rows[i]["N_JINE"]);
                    }

                    e.Result = e.Parameter + "|0|保存成功！|" + zje + "|" + this.dtYinHangZ.Rows.Count;
                }
                else
                {
                    e.Result = e.Parameter + "|0|保存失败！";
                }
            }
        }

        /// <summary>
        /// grid数据表刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvYinHangZCL_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }
    }
}